<template>
  <div class="coupon-records">
    <el-table :data="recordList" border>
      <el-table-column prop="id" label="记录ID" width="80" />
      <el-table-column prop="cname" label="优惠券ID" width="80" />
      <el-table-column prop="uname" label="用户ID" width="80" />
      <el-table-column prop="orderId" label="订单ID" width="120" />
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ formatStatus(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="amountUsed" label="使用金额" />
      <el-table-column prop="receivedAt" label="领取时间" />
      <el-table-column prop="usedAt" label="使用时间" />
      <el-table-column prop="expiredAt" label="过期时间" />
    </el-table>

    <el-pagination
      :current-page="page.current"
      :page-size="page.size"
      :total="page.total"
      layout="total, prev, pager, next"
      @current-change="handlePageChange"
    />
  </div>
</template>

<script>
import { listCouponRecord } from '@/api/coupon/couponRecord'

export default {
  name: 'CouponRecords',
  data() {
    return {
      recordList: [],
      page: {
        current: 1,
        size: 10,
        total: 0
      }
    }
  },
  created() {
    this.fetchRecords()
  },
  methods: {
    formatStatus(status) {
      const statusMap = {
        'UNUSED': '未使用',
        'USED': '已使用',
        'EXPIRED': '已过期',
        'INVALID': '已作废'
      }
      return statusMap[status] || status
    },
    getStatusType(status) {
      const typeMap = {
        'UNUSED': 'info',
        'USED': 'success',
        'EXPIRED': 'warning',
        'INVALID': 'danger'
      }
      return typeMap[status] || 'info'
    },
    async fetchRecords() {
      try {
        const { data } = await listCouponRecord({
          pageNum: this.page.current,
          pageSize: this.page.size
        })
        this.recordList = data.records
        this.page.total = data.total
      } catch (error) {
        this.$message.error('获取优惠券记录失败')
      }
    },
    handlePageChange(page) {
      this.page.current = page
      this.fetchRecords()
    }
  }
}
</script>

<style scoped>
.coupon-records {
  padding: 20px;
}
</style> 